package com.oodso.sell.model;

import com.oodso.sell.BuildConfig;
import com.oodso.sell.model.util.DuoshuInterceptor;
import com.oodso.sell.model.util.MouseInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 项目名称：ConsumSostroe
 * 类描述：
 * 创建人：资拥
 * 创建时间：16/10/26 下午3:17
 * 修改人：赵宁
 * 修改时间：16/10/26 下午3:17
 * 修改备注：
 */
public class SellHttp {
//        public static final boolean debug = false;
    public static final boolean debug = BuildConfig.DEBUG;
    private static final int DEFAULT_TIMEOUT = 10;

    public static final String BASE_HOST = debug ? "sandbox.api.oodso.com" : "a.oodso.com";
    public static final String APPTOH5_HOST = debug ? "test.app.oodso.com" : "app.oodso.com";
    public static final String BASE_HOST1310 = debug ? "test.e.oodso.com" : "a.oodso.com";
    public static final String BASE_H5_URL = debug ? "http://test.platform.oodso.com" : "https://platform.oodso.com";


//    public static final String BASE_HOST = debug?"sandbox.api.oodso.com":"uat-api.oodso.com";
//    public static final String APPTOH5_HOST =debug? "test.app.oodso.com":"app.oodso.com";
//    public static final String BASE_HOST1310 = debug?"test.e.oodso.com":"uat-api.oodso.com";

    public static final String APPTOH5_URL = "http://" + APPTOH5_HOST + "/router/rest/";
    public static final String BASE_URL = "http://" + BASE_HOST + "/router/rest/";
    public static final String BASE_URL1310 = "http://" + BASE_HOST1310 + "/router/rest/";


    public static final String GOOD_PREVIEW_URL = "http://" + APPTOH5_HOST + "/html/share2/shop-goods.html?id=";

    private SellApi mSellApi = null;
    private AppToH5Api appToH5Api = null;
    private MySellApi mySellApi = null;
    private SellApi1310 sellApi1310 = null;

    private SellHttp() {
        OkHttpClient.Builder okhttpBuilder = new OkHttpClient.Builder();
        okhttpBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        okhttpBuilder.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        okhttpBuilder.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        okhttpBuilder.addInterceptor(new MouseInterceptor());
        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
            logging.setLevel(HttpLoggingInterceptor.Level.BODY);
            okhttpBuilder.addInterceptor(logging);
        }
        Retrofit retrofit = new Retrofit.Builder().client(okhttpBuilder.build())
                .addConverterFactory(GsonConverterFactory
                        .create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory
                        .create())
                .baseUrl(BASE_URL).build();
        mSellApi = retrofit.create(SellApi.class);

        Retrofit AppToH5Retrofit = new Retrofit.Builder()
                .client(okhttpBuilder.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(APPTOH5_URL).build();
        appToH5Api = AppToH5Retrofit.create(AppToH5Api.class);


        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        builder.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        builder.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        builder.addInterceptor(new DuoshuInterceptor());

        Retrofit retrofit2 = new Retrofit.Builder().client(builder.build())
                .addConverterFactory(GsonConverterFactory
                        .create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory
                        .create())
                .baseUrl(BASE_URL).build();
        mySellApi = retrofit2.create(MySellApi.class);

        Retrofit retrofit3 = new Retrofit.Builder().client(okhttpBuilder.build())
                .addConverterFactory(GsonConverterFactory
                        .create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory
                        .create())
                .baseUrl(BASE_URL1310).build();
        sellApi1310 = retrofit3.create(SellApi1310.class);

    }


    private static class SingletonHolder {
        static SellHttp INSTANCE = new SellHttp();
    }

    public static SellHttp getInstance() {
        return SingletonHolder.INSTANCE;
    }


    public SellApi getSellApi() {
        return mSellApi;
    }

    public MySellApi getMySellApi() {
        return mySellApi;
    }

    public AppToH5Api getAppToH5lApi() {
        return appToH5Api;
    }

    public SellApi1310 getSellApi1310() {
        return sellApi1310;
    }

}
